<template>
  <div style="min-height: 100vh">
    <div class="card-tab">
      <div class="tab"
           v-for="(item,index) in tabList"
           :key="index"
           :class="{'active':item.active}"
           @click="changeTab(item)">
        <span>{{item.name}}</span>
      </div>
    </div>

    <div v-if="tabShow == 1">
      <!-- <div class="List">
        <div class="card">
          <img class="card-bg" src="/static/images/card-normal-bg.png"/>
          <div class="card-main">
            <div class="left">
              <div class="text1">骑行卡</div>
              <div class="text2">周卡</div>
              <div class="text2">2020-01-22 22:22 至 2020-01-22 22:22</div>
            </div>
          </div>
        </div>
        <div class="card">
          <img class="card-bg" src="/static/images/card-used-bg.png"/>
          <div class="card-main">
            <div class="left">
              <div class="text1">骑行卡</div>
              <div class="text2">周卡</div>
              <div class="text2">2020-01-22 22:22 至 2020-01-22 22:22</div>
            </div>
          </div>
        </div>
      </div> -->
      <div class="List" v-if="cardList.length>0">
        <div v-for="(item,index) in cardList" :key="index" class="card">
          <img class="card-bg" :src="item.imgUrl"/>
          <div class="card-main" :class="{expiredFlag:item.expiredFlag == 1}">
            <div class="left">
              <div class="text1">{{item.cardName}}</div>
              <div class="text2">{{item.remaining}}</div>
            </div>
            <div class="right">
              <div class="text3" v-if="item.expiredFlag == 0">{{item.endTimeStr}}</div>
              <div class="text3" v-if="item.expiredFlag == 1"><span>已过期</span></div>
              <div class="text4" v-if="item.rightDownImgUrl" @click="jumpDetail(item.unlimitedCardId)"><img :src="item.rightDownImgUrl"></div>
            </div>
          </div>
        </div>
      </div>
      <div class="nodata" v-if="cardList.length == 0">
        <div>
          <img src="/static/images/noCard.png" class="none"/>
          <dt class="tip2">暂无卡片</dt>
        </div>

      </div>
    </div>
    <div v-if="tabShow == 2">
      <div class="List" v-if="couponsList.length>0">
        <div class="card" v-for="(item,index) in couponsList" :key="index" >
          <img class="card-bg" src="https://klcx-bucket.oss-cn-beijing.aliyuncs.com/quan/zhekou.png" v-if="item.isExpired == 0"/>
          <img class="card-bg" src="https://klcx-bucket.oss-cn-beijing.aliyuncs.com/quan/zhekouuse.png" v-if="item.isExpired == 1"/>
          <div class="card-main">
            <div class="left">
              <div class="text1">{{item.title}}</div>
              <div class="text2">{{item.amountStr}}</div>
              <div class="text2">{{item.startTime}}{{item.endTime}}</div>
            </div>
          </div>
        </div>
      </div>
      <!-- <div class="List" v-if="couponsList.length>0">
        <div v-for="(item,index) in couponsList" :key="index" class="coupons"  :class="{expiredFlag:item.isExpired == 1}">
          <div class="coupons-price">
            <img class="priceBg" :src="item.imgUrl">
            <span class="price-text">{{item.amountStr}}</span>
          </div>
          <div class="coupons-info">
            <div class="coupons-name">{{item.title}}</div>
            <div class="coupons-time">{{item.startTime}}{{item.endTime}}</div>
            <div class="coupons-tip">{{item.title}}</div>
          </div>
          <img v-if="item.isExpired == 1" class="expiredImg" src="https://static.meboth.cn/xiaomi/metripapp/wechat-app-xiaomi/images/expired.png">
        </div>
      </div> -->
      <div class="nodata" v-if="couponsList.length == 0">
        <div>
          <img src="/static/images/noCoupons.png" class="none"/>
          <dt class="tip2">暂无优惠券</dt>
        </div>

      </div>
    </div>
  </div>
</template>

<script>
  import Service from '@/utils/service'

  export default {

    data() {
      return {
        tabList: [
          {
            active: true,
            imgName: 'cardTab',
            name: '骑行卡'
          },
          {
            active: false,
            imgName: 'couponsTab',
            name: '骑行券'
          }
        ],
        tabShow: 1,
        cardList: [],
        couponsList: [],
        page: 1,

      }
    },
    onUnload() {
      Object.assign(this.$data, this.$options.data())
    },
    onLoad() {
      let userInfo = wx.getStorageSync('userInfo')
      if (!userInfo) {
        wx.redirectTo({
          url: '../login/main'
        })
        return
      }
      this.cardList = []
      this.couponsList = []
      this.page=1
      this.getCardList(1)
    },
    onReachBottom() {
      if (this.tabShow == 2) {
        this.getCouponsList(this.page)
      }
    },
    methods: {
      jumpDetail(id) {
        wx.navigateTo({
          url: `../myCardDetail/main?unlimitedCardId=${id}`
        })
      },
      changeTab(item) {
        this.tabList.forEach((item) => {
          item.active = false
        })
        item.active = true
        this.cardList = []
        this.couponsList = []
        this.page=1
        if (item.name == '骑行卡') {
          this.tabShow = 1
          this.getCardList(1)
        } else {
          this.tabShow = 2
          this.getCouponsList(1)
        }
      },
      getCardList(page) {
        let params = {pageNum: page, pageSize: 200}
        Service.getCardList(params)
          .then((res) => {
            if (res.code == 200) {
              if (res.data) {
                this.cardList = res.data
//                var arr = this.cardList
//                for (var i = 0; i < res.data.length; i++) {
//                  arr.push(res.data[i])
//                }
//                this.cardList = arr
//                if (res.data.length > 0) {
//                  this.page++;
//                }
              }
            }
          })
      },
      getCouponsList(page) {
        let params = {
          couponType: '1', //我的卡券处优惠券
          pageNum: page,
          pageSize: 10
        }
        Service.getCoupons(params)
          .then((res) => {
            if (res.code == 200) {
              if (res.data) {
                var arr = this.couponsList
                for (var i = 0; i < res.data.length; i++) {
                  arr.push(res.data[i])
                }
                this.couponsList = arr
                if (res.data.length > 0) {
                  this.page++;
                }
              }
            }
          })
      },
    }
  }
</script>

<style scoped>
  .coupons{
    position: relative;
    width:690px;
    height:167px;
    background:rgba(255,255,255,1);
    border-radius:10px;
    padding: 5px;
    box-sizing: border-box;
    margin-bottom: 30px;
    display: flex;
  }
  .coupons-price{
    position: relative;
    width: 197px;
  }
  .priceBg{
    width: 197px;
    height: 157px;
  }
  .price-text{
    position: absolute;
    top:0;
    left: 0;
    width: 197px;
    height: 157px;
    line-height: 157px;
    font-size:48px;
    font-weight:500;
    color:rgba(51,51,51,1);
    text-align: center;
  }
  .coupons-info{
    padding:15px 0 0 20px;
    flex:1;
  }
  .coupons-name{
    font-size: 30px;
    font-weight:500;
    color:rgba(51,51,51,1);
  }
  .coupons-time{
    padding-top: 5px;
    font-size: 26px;
    color:#999;
  }
  .expiredImg{
    position: absolute;
    top: 0;
    right: 0;
    width: 88px;
    height: 88px;
  }
  .expiredFlag .coupons-name{
    color: #999;
  }
  .card{
    position: relative;
    margin-bottom: 30px;
  }
  .card-bg{
    width: 706px;
    height: 168px;
    vertical-align: top;
  }
  .card-main{
    position: absolute;
    left:0;
    top:0;
    width: 706px;
    height: 168px;
    display: flex;
    justify-content: space-between;
  }
  .card-main .left{
    overflow: hidden;
    padding-left: 150px;
  }
  .card-main .right{
    padding-right: 50px;
    text-align: right;
  }
  .text1{
    padding: 10px 0 0 0;
    font-size:36px;
    font-weight:500;
    line-height: 50px;
    color:#333;
  }
  .text2{
    padding-top: 10px;
    font-size:26px;
    font-weight:400;
    color:#999999;
    line-height:37px;
  }
  .text3{
    padding: 25px 0 0 0;
    font-size:26px;
    font-weight:400;
    color:rgba(255,255,255,1);
    line-height: 50px;
  }
  .text3 span{
    display: inline-block;
    width:129px;
    height:50px;
    background:rgba(229,229,229,1);
    border-radius:25px;
    font-size:26px;
    font-weight:400;
    color:rgba(153,153,153,1);
    line-height:50px;
    text-align: center;
  }
  .text4 img{
    margin-top: 78px;
    width: 128px;
    height: 37px;
  }
  .expiredFlag .text1,.expiredFlag .text2{
    color: #999;
  }
  .card-tab{
    display: block;
    width: 100%;
    border-top: 1px solid #F6F6F6;
    height: 80px;
    line-height: 80px;
    box-sizing: border-box;
  }
  .card-tab .tab{
    display: block;
    float: left;
    width: 150px;
    text-align: center;
    font-size: 30px;
    color: #999999;
  }
  .card-tab .tab.active{
    border-bottom: solid 4px #000000;
    color: #333;
  }
  .card-tab img{
    width: 40px;
    height: 30px;
    display: inline-block;
    vertical-align: middle;
    margin-right: 15px;
  }
  .view {
    text-align: center;
    padding: 0;
    margin: 0;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
  }

  .nodata {
    width: 100%;
    min-height: calc(100vh - 80px);
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    background: #F6F6F6;
  }



  .List {
    position: relative;
    width: 100%;
    min-height: calc(100vh - 80px);
    padding: 30px;
    background: #F6F6F6;
    box-sizing: border-box;
    margin-top: 8px;
  }


  .List dt {
    position: relative;
    margin-bottom: 30px;
  }

  .List dt:last-child {
    margin-bottom: 0;
  }


  .none {
    width: 80px;
    height: 80px;
    text-align: center;
  }
  .tip2{
    font-size:30px;
    font-weight:400;
    color:rgba(153,153,153,1);
    line-height:42px;
  }

</style>
